<?php

namespace App\Admin\Controllers;

use App\Models\Job;

use App\Models\Sector;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class JobController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('招聘管理');
//            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('招聘管理');
//            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('招聘管理');
//            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Job::class, function (Grid $grid) {
            $grid->disableExport();
            $grid->id('ID')->sortable();

            $grid->column('name', '招聘职位');
            $grid->column('sector_id', '职位部门')->display(function ($value){
                $sector = Sector::select('id', 'name')->find($value);
                return $sector ? $sector->name : '部门不存在';
            });

//            $grid->column('describe', '职位描述')->display(function ($value) {
//                return str_limit($value, 30, '...');
//            });

            $grid->column('explain', '其他说明');
//            $grid->column('telephone', '联系电话');

            $grid->created_at('添加时间');
//            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Job::class, function (Form $form) {

//            $form->display('id', 'ID');

            $form->text('name', '招聘职位')
                ->attribute('maxlength', 10)
                ->help('说明：10个中文字符以内')
                ->rules('required|max:10');

            $sectors = Sector::select('id', 'name')->get()->pluck('name', 'id');

            $form->select('sector_id', '职位部门')
                ->options($sectors)
                ->rules('required');;

            $form->editor('describe', '职位描述')
                ->attribute('maxlength', 500)
                ->help('说明：500个中文字符以内')
                ->rules('required');

            $form->text('explain', '其他说明')
                ->help('说明：多个标签用英文逗号隔开')
                ->rules('required');

            $form->editor('telephone', '联系方式')
                ->attribute('maxlength', 200)
                ->help('说明：200个中文字符以内')
                ->rules('required');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
